/**
 * EasyAjax
 * author ：jyolo
 * email  : 438608613@qq.com
 * 使用方法
 * 标签属性 ：
 * data-url 请求地址 必填
 * data-content 请求数据内容 必填 example : "{id:'123','key':'234'}"
 * data-confirm 用户确定信息 选填 增加一个 确定的提示 通常用于删除 等敏感操作
 * data-config  请求设定 选填（默认post） example ：{method:'get'} 该属性用户设置ajax的配置 比如 method ,async等具体可参考手册
 */
/**
 * 打印函数
 * @param o
 */
function p(o) {
    console.log(o);
}
/**
 * 回调函数
 * @param dom
 */
function table_del(dom) {
    //如果当前页面tabel 只有一个元 并且删除的话 那就直接跳转上一页
    if($(dom).parents('tr').siblings().length == 1){
        var page = window.location.href.match(/page=(\d+)/i)[1];
        $(dom).parents('tr').remove();
        var newpage = parseInt(page)-1;
        if(newpage > 0){
            var href = window.location.href.replace(/page=(\d+)/,'page='+newpage);
            window.location.href=href;
        }
    }else{ //否则直接删除当前节点
        $(dom).parents('tr').remove();
    }

}
;(function($){
    var settting = {
        class_name : '.ajax', //默认的元素class 名称,可在设置里面自行更改，或者在 init传入的设置
    };
    var ajaxinfo = {};var load = false;

    var run = function (dom) {
        var _this = $(dom);

        ajaxinfo.url = _this.attr('data-url');
        ajaxinfo.data = new Function("return "+_this.attr('data-content')+"")();
        ajaxinfo.config = new Function("return "+_this.attr('data-config')+"")();
        ajaxinfo.callback = _this.attr('data-call-back') ?_this.attr('data-call-back'):false;
        try {
            ajaxinfo.callback = new Function("return "+ajaxinfo.callback+"")();
        }catch (e){
            tips(ajaxinfo.callback + '回调函数不存在');
        }

        if(typeof ajaxinfo.config == 'undefined'){
            ajaxinfo.config = {method:'post',async:true};
        }
        confirm_info = _this.attr('data-confirm') ? _this.attr('data-confirm'):false;
        if(confirm_info){
            var pass = false;
            $.dialog({
                type : 'confirm',
                onClickOk : function(){
                    pass = true;
                },
                onClickCancel : function(){
                    pass = false;
                },
                onClosed : function(){
                    if(pass == true){
                        window.setTimeout(function () {
                            doajax(_this);
                            pass = false;
                        }, 300);
                    }
                },
                contentHtml : "<p style='text-align: center'>"+confirm_info+"</p>",
            });
        }else{
            doajax();
        }


    };
    var loading = function (content,code) {
        if(load == false){
            load = $.dialog({
                type:'info',
                infoText : content,
                infoIcon:'/static/public/images/icon/loading.gif',
            });
        }else{
            switch(code){
                case 0:
                    var icon = '/static/public/images/icon/fail.png';
                    break;
                case 1:
                    var icon = '/static/public/images/icon/success.png';
                    break;
            }
            load.dialog.update({
                autoClose : 600,
                infoText : content,
                infoIcon : icon,
            });
        }
    };
    var tips = function(content,code){

        switch(code){
            case 0:
                var icon = '/static/public/images/icon/fail.png';
                break;
            case 1:
                var icon = '/static/public/images/icon/success.png';
                break;
        }
        $.dialog({
            type:'tips',
            autoClose:1500,
            infoText : content,
            infoIcon:icon,
        });
    };
    var doajax = function (dom) {
        $.ajax.prototype.dom = dom;
        $.ajax({
            url:ajaxinfo.url,
            data:ajaxinfo.data,
            method:ajaxinfo.config.method,
            async:ajaxinfo.config.async,
            beforeSend:beforeSend_handle,
            success:success_handle,
            complete:complete_handle,
        });

    };
    var beforeSend_handle = function () {
        loading('加载中');
    };
    var success_handle = function (res) {
        loading(res.msg,res.code);
        var dom = $.ajax.prototype.dom;
        ajaxinfo.callback(dom);
    };
    
    var complete_handle = function () {
        load = false;
    };


    $.fn.EasyAjax = {
        init:function (set) {

            if(typeof set == 'object')$.extend(settting,set);

            $(settting.class_name).each(function (i,n) {
                $(n).on('click',function () {run(n);return false;})
            })
        }
    };
})(window.jQuery || window.Zepto || window.$);
$(function(){
    $.fn.EasyAjax.init()
});